package com.xizi.queue;

import java.util.concurrent.ArrayBlockingQueue;

class PutThread extends Thread {
    private ArrayBlockingQueue<Integer> abq;

    public PutThread(ArrayBlockingQueue<Integer> abq) {
        this.abq = abq;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("put " + i);
                abq.put(i);
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class GetThread extends Thread {
    private ArrayBlockingQueue<Integer> abq;

    public GetThread(ArrayBlockingQueue<Integer> abq) {
        this.abq = abq;
    }

    public void run() {
        for (int i = 0; i < 15; i++) {
            try {
                System.out.println("take " + abq.take());
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class ArrayBlockingQueueDemo {
    public static void main(String[] args) {
        ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(10);
        PutThread p1 = new PutThread(abq);
        GetThread g1 = new GetThread(abq);
        p1.start();
        g1.start();
    }
}